﻿
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using Swaf;
using Swaf.Gui;

using Swaf.BizObj;
using Swaf.Container;
using Swaf.Security;

namespace Calendars.Gui
{
	public class CalendarEventSearchBizProc : BaseBusinessProcess
	{
		public CalendarEventSearchBizProc(ActiveBizProc procState)
			: base(procState)
		{
			if (State.InProgressAddBizObj == null)
				State.InProgressAddBizObj = createNewBizObj();
		}

		protected override void setupRuleNames()
		{
		}

		public override Privileges CurrentEditPrivilege
		{
			get
			{
				Privileges priv = Privileges.Add;
				return priv;
			}
		}

		public override IBizObj createNewBizObj()
		{
			IBizObj n = base.createNewBizObj();
			DateTime nw = DateTime.UtcNow;
			nw = new DateTime(nw.Year, nw.Month, 1, 0, 0,0);
			nw = nw.AddMonths(-1);
			n.put("startTime", nw);
			nw = nw.AddMonths(7);
			n.put("endtime", nw);

			return n;
		}

		protected override string BizObjTypeName { get { return "CalendarSearch"; } }

		public override IResults updateBizObj(IBizObj obj)
		{
			ActiveBizProc pushCrit = m_procState.getParentBizProc("pushCriteria");
			if (pushCrit != null)
			{
				pushCrit.BizProc.FilterSortCriteria = getFilterSortCriteria(obj,pushCrit.BizProc.FilterSortCriteria);
				pushCrit.BizProc.clearRetrievedData();
			}
			return null;
		}
		protected IBizObj getFilterSortCriteria(IBizObj obj, IBizObj filterOp)
		{
			filterOp = App.bizObjMgr.create("FilterSortOptions");
			if (CurrentBizObj != null)
			{
				filterOp.put("filterIsAnd", true);
				IBizObjList filters = filterOp.get("filterOptions") as IBizObjList;

				IBizObj filt = filters.insertNew();
				filt.put("field", "cal.startTime");
				filt.put("criteria", ">=");
				filt.put("value", CurrentBizObj.get("startTime"));

				filt = filters.insertNew();
				filt.put("field", "cal.endTime");
				filt.put("criteria", "<=");
				filt.put("value", CurrentBizObj.get("endTime"));
			}
			return filterOp;
		}
		public override void updateBizObj(bool refreshOnly)
		{
			IBizObj srch = State.InProgressAddBizObj;
			base.updateBizObj(refreshOnly);
			State.InProgressAddBizObj = srch;
		}

		public override void cancelEdits()
		{
			updateBizObj(State.InProgressAddBizObj = createNewBizObj());
			
		}


	}
}
